package com.ecoolex.chinadutyfree.retailer.api.graphql;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.ecoolex.chinadutyfree.retailer.entity.DeliveryManner;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoods;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsProduct;
import com.ecoolex.chinadutyfree.retailer.entity.FreightTemplate;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsProduct;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsWithBLOBs;
import com.ecoolex.chinadutyfree.retailer.ws.DeliveryService;
import com.ecoolex.chinadutyfree.retailer.ws.DirectMailGoodsService;
import com.ecoolex.chinadutyfree.retailer.ws.GoodsService;
import com.ecoolex.framework.common.annotation.Description;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.modularize.api.service.currency.CurrencyType;
import com.ecoolex.framework.modularize.api.service.currency.CurrencyTypeService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;

@GraphQL2Module(type = Type.User, description = "直邮商品")
public class DirectMailGoodsGraphql {

	@Reference
	private DirectMailGoodsService service;

	@Reference
	private GoodsService goodsService;

	@Reference
	private DeliveryService deliveryService;
	
	@Autowired
	private CurrencyTypeService currencyTypeService;
	
	@TypeField(type = DirectMailGoods.class, field = "currencyType", description = "币种")
	public CurrencyType currencyType(@Source DirectMailGoods source) {
		return currencyTypeService.get(source.getCurrencyType());
	}

	@TypeField(type = DirectMailGoods.class, field = "goods", description = "商品")
	public GoodsWithBLOBs goods(@Source DirectMailGoods source) {
		return goodsService.getGoodsById(source.getGoodsId());
	}

	@TypeField(type = DirectMailGoods.class, field = "freightTemplate", description = "运费模板")
	public FreightTemplate freightTemplate(@Source DirectMailGoods source) {
		return deliveryService.getFreightTemplateById(source.getFreightTemplateId());
	}

	@TypeField(type = DirectMailGoods.class, field = "deliveryMannerList", description = "配送方式集合")
	public List<DeliveryManner> deliveryMannerList(@Source DirectMailGoods source) {
		return service.queryDeliveryMannerByDirectMailGoodsId(source.getDirectMailGoodsId()).stream().map(
			dm -> deliveryService.getDeliveryMannerById(dm.getDeliveryMannerId())).collect(Collectors.toList());
	}

	@TypeField(type = DirectMailGoods.class, field = "productList", description = "产品集合")
	public List<DirectMailGoodsProduct> productList(@Source DirectMailGoods source) {
		return service.queryNormalProductByDirectMailGoodsId(source.getDirectMailGoodsId());
	}

	@TypeField(type = DirectMailGoodsProduct.class, field = "product", description = "产品")
	public GoodsProduct product(@Source DirectMailGoodsProduct source) {
		return goodsService.getGoodsProductById(source.getProductId());
	}

	@QueryField(field = "recommendDirectMailGoodsList", description = "查询推荐直邮商品列表")
	public Pagination<DirectMailGoods> queryRecommend(@Description({
		"直邮商品位置" }) @NonNull String position, @Description({
			"直邮商品位置的值" }) @NonNull String[] positionValue, @NonNull Sorting[] sorts, @NonNull Page page) {
		return service.queryRecommendDirectMailGoodsForH5(position, positionValue, sorts, page);
	}

	@QueryField(field = "directMailGoodsList", description = "查询直邮商品列表")
	public Pagination<DirectMailGoods> query(String customName, @NonNull Sorting[] sorts, @NonNull Page page) {
		return service.queryDirectMailGoodsForH5(customName, sorts, page);
	}

	@QueryField(field = "directMailGoodsListByCategoryId", description = "查询二级分类下的直邮商品列表")
	public Pagination<DirectMailGoods> queryByGoodsCategory(@NonNull Integer categoryId, @NonNull Sorting[] sorts, @NonNull Page page) {
		return service.queryDirectMailGoodsByCategoryIdForH5(categoryId, sorts, page);
	}
	
	@QueryField(field = "directMailGoodsListByFirstCategoryId", description = "查询一级分类下的直邮商品列表")
	public Pagination<DirectMailGoods> queryByFirstGoodsCategory(@NonNull Integer categoryId, @NonNull Sorting[] sorts, @NonNull Page page) {
		return service.queryDirectMailGoodsByFirstCategoryIdForH5(categoryId, sorts, page);
	} 

	@QueryField(field = "directMailGoods", description = "根据直邮商品id查询详情")
	public DirectMailGoods getById(@NonNull Integer directMailGoodsId) {
		return service.getByIdForH5(directMailGoodsId);
	}
	
	@QueryField(field = "directMailGoodsListByBrandId", description = "根据品牌Id查询直邮商品列表")
	public Pagination<DirectMailGoods> queryByBrandId(String name,@NonNull Integer brandId,@NonNull Sorting[] sorts,@NonNull Page page){
		return service.queryByBrandId(name,sorts,brandId, page);
	}
}
